import 'xgplayer/dist/index.min.css'
import Player from 'xgplayer'
import { ImagePreview } from 'vant'
import { FileTypes } from '@/components/FilePreviewer'
import { openUrlByBrowser } from './openUrlByBrowser'

class AbstractPreviewer {
  constructor(selector) {
    this.selector = selector
    this.player = null
    this.sourceUrl = ''
  }

  preview(fileType, url) {
    if (FileTypes.图片.split(',').includes(fileType)) {
      ImagePreview({ images: [url], showIndex: false })
      return
    }
    if (fileType === FileTypes.文档) {
      return this.previewPDF(url)
    }
    if (FileTypes.音视频.split(',').includes(fileType)) {
      return this.previewMedia(url)
    }
    return this.previewOthers(url)
  }

  previewMedia(url) {
    if (this.player && this.sourceUrl === url) {
      return this.player
    }
    this.sourceUrl = url
    if (this.player) {
      this.player.destroy()
    }
    const options = {
      id: this.selector,
      url,
      width: '100vw',
      height: '100vh',
      videoFillMode: 'contain'
    }
    this.player = new Player(options)
    return this.player
  }

  // 子类实现
  previewPDF(url) {}

  previewOthers(url) {
    this.sourceUrl = url
    openUrlByBrowser(url)
  }

  destroy() {
    if (this.player) {
      this.player.destroy()
    }
    this.player = null
  }
}

class AppPreviewer extends AbstractPreviewer {
  // constructor(selector) {
  //   super(selector);
  // }

  previewPDF(url) {
    // window.yyn.invokeJSB('openWindow', {
    //   openUrl: `https://static.ybsjyyn.com/pdf-viewer/viewer.html?file=${url}`,
    //   isFullScreen: 0,
    //   needLoading: 1
    // });
    this.previewOthers(url)
  }
}

export function createRuntimePreviewer() {
  return AppPreviewer
}
